package com.fram.controller.api;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.fram.bean.api.distributor.request.DistributorFarmRequest;
import com.fram.bean.api.distributor.request.DistributorUpdateRequest;
import com.fram.bean.sys.PageBean;
import com.fram.bean.sys.ResponseMsg;
import com.fram.common.canstant.ErrCode;
import com.fram.common.exception.ProAffairException;
import com.fram.common.exception.ProException;
import com.fram.common.util.PublicMethod;
import com.fram.common.util.apiversion.ApiVersion;
import com.fram.common.util.base.BaseController;
import com.fram.entity.DistributorFarm;
import com.fram.service.api.v1.DistributorService;

/**
 * @作者：大晓刚 @时间 2020年5月19日 @dascription: ->前台-店铺 查询功能
 */
@RestController
@RequestMapping("/api/{version}/distributor")
public class DistributorController extends BaseController {

	private static Logger LOG = LoggerFactory.getLogger(DistributorController.class);

	@Autowired
	private DistributorService distributorService;

	@ApiVersion(1.0)
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	private ResponseMsg list(@RequestBody PageBean<DistributorFarmRequest> request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(distributorService.list(request));
		} catch (ProException e) {
			LOG.error("-店铺-更多分销商出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-店铺-更多分销商异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/**
	 * 店铺查询
	 * @param request
	 * @return
	 */
	@ApiVersion(1.0)
	@RequestMapping(value = "/detail", method = RequestMethod.POST)
	private ResponseMsg getDistributorFarm(@RequestBody DistributorFarmRequest request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			try {
				request.setUserId(getApiUserId());
			} catch (Exception e) {
			}
			msg.setData(distributorService.selectDistributorFarm(request));
		} catch (ProException e) {
			LOG.error("-店铺-根据经纬度查询出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-店铺-根据经纬度查询异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	@ApiVersion(1.0)
	@RequestMapping(value = "auth/apply/detail")
	private ResponseMsg distributorApplyDetail() {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(distributorService.distributorApplyDetail(getApiUserId()));
		} catch (ProException e) {
			LOG.error("-店铺-用户申请合伙人信息出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-店铺-用户申请合伙人信息异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	@ApiVersion(1.0)
	@RequestMapping(value = "auth/apply/add")
	private ResponseMsg distributorApply(@RequestBody DistributorFarm request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			request.setAdminUserId(getApiUserId());
			msg.setData(distributorService.distributorApply(request));
		} catch (ProException e) {
			LOG.error("-店铺-合伙人申请出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-店铺-合伙人申请异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	@ApiVersion(1.0)
	@RequestMapping(value = "auth/apply/update")
	private ResponseMsg distributorApplyUpdate(@RequestBody DistributorFarm request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			request.setAdminUserId(getApiUserId());
			msg.setData(distributorService.distributorApplyUpdate(request));
		} catch (ProException e) {
			LOG.error("-店铺-合伙人申请修改出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-店铺-合伙人申请修改异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/**
	 * 前端用户切换身份为分销商
	 * 
	 * @param request
	 * @return
	 */
	@ApiVersion(1.0)
	@RequestMapping(value = "auth/switch/identity")
	private ResponseMsg distributorFarmDetail() {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(distributorService.distributorFarmDetail(getApiUserId()));
		} catch (ProException e) {
			LOG.error("-店铺-前端用户切换身份为分销商出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-店铺-前端用户切换身份为分销商异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/**
	 * 前端用户修改分销商
	 * 
	 * @param request
	 * @return
	 */
	@ApiVersion(1.0)
	@RequestMapping(value = "auth/update")
	private ResponseMsg updatedDistributor(@RequestBody DistributorUpdateRequest request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			request.setAdminUserId(getApiUserId());
			distributorService.updatedDistributor(request);
		} catch (ProAffairException e) {
			LOG.error("-店铺-前端用户修改分销商出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-店铺-前端用户修改分销商异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

}
